﻿#Region "Copyright"

' Copyright 2007 Ben Reichelt - http://benreichelt.net
' 
' Licensed under the Apache License, Version 2.0 (the "License");
' you may not use this file except in compliance with the License.
' You may obtain a copy of the License at
' 
'     http://www.apache.org/licenses/LICENSE-2.0
' 
' Unless required by applicable law or agreed to in writing, software
' distributed under the License is distributed on an "AS IS" BASIS,
' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' See the License for the specific language governing permissions and
' limitations under the License.


#End Region

Imports NUnit.Framework
Imports SqlPlastic
Imports System.Xml.Linq

<TestFixture()> _
Public Class MetaDataAssociationTests

    Private metaData As XmlMetaData

    <SetUp()> _
    Public Sub Setup()
        metaData = ObjectMother.CreateDefaultMetaData
    End Sub

    <Test()> _
    Public Sub RenameAssociationTest()
        Dim type As MetaDataType = metaData.FindType(C.Type1Name)
        Dim assoc As MetaDataAssociation = type.Association(C.Association1Name)
        assoc.RenameTo("Ben")
        Assert.AreEqual("Ben", assoc.MemberName)
        assoc = type.Association(C.Association1Name)
        Assert.AreEqual("Ben", assoc.MemberName)
    End Sub

    <Test()> _
    Public Sub RenameAssociationMissingAttributeTest()
        Dim type As MetaDataType = metaData.FindType(C.Type1Name)
        Dim assoc As MetaDataAssociation = type.Association(C.AssociationWithoutMember)
        assoc.RenameTo("Ben")
        Assert.AreEqual("Ben", assoc.MemberName)
        assoc = type.Association(C.AssociationWithoutMember)
        Assert.AreEqual("Ben", assoc.MemberName)
    End Sub

    <Test()> _
    Public Sub ChangeOtherKeyTest()
        Dim list As List(Of MetaDataAssociation) = metaData.FindAllAssociations()
        Assert.Greater(list.Count, 0)
        list(0).ChangeOtherKey("Ben")
        Assert.AreEqual("Ben", list(0).OtherKey)
    End Sub

    <Test()> _
    Public Sub ChangeThisKeyTest()
        Dim list As List(Of MetaDataAssociation) = metaData.FindAllAssociations()
        Assert.Greater(list.Count, 0)
        list(0).ChangeThisKey("Ben")
        Assert.AreEqual("Ben", list(0).ThisKey)
    End Sub

    <Test()> _
    Public Sub GetOtherKeyDoesntExist()
        Dim type As MetaDataType = metaData.FindType(C.Type1Name)
        Dim assoc As MetaDataAssociation = type.Association(C.AssociationWithNoOtherKey)
        Assert.AreEqual(String.Empty, assoc.OtherKey)
    End Sub

    <Test()> _
    Public Sub GetThisKeyDoesntExist()
        Dim type As MetaDataType = metaData.FindType(C.Type1Name)
        Dim assoc As MetaDataAssociation = type.Association(C.AssociationWithNoThisKey)
        Assert.AreEqual(String.Empty, assoc.ThisKey)
    End Sub

    <Test()> _
    Public Sub GetMemberNameDoesntExist()
        Dim type As MetaDataType = metaData.FindType(C.Type1Name)
        Dim assoc As MetaDataAssociation = type.Association(C.AssociationWithoutMember)
        Assert.AreEqual(String.Empty, assoc.MemberName)
    End Sub

End Class
